const { think } = require('thinkjs');
const Base = require('./base.js');
const moment = require('moment/moment.js');
const { default: BigNumber } = require('bignumber.js');

module.exports = class extends Base {
  async panelAction() {
    const area = await this.model('project_list')
      .where({ status: 2, type: 4 })
      .sum('area');
    const total = await this.model('project_list')
      .where({ type: 4 })
      .count();
    const rest = await this.model('project_list')
      .where({ type: 4, status: 1 })
      .sum('area');

    const totalarea = await this.model('project_list')
      .where({ type: 4, status: ['IN', '1,2'] })
      .sum('area');

    const k = await this.model('project_list')
      .where({ type: 4, status: 1 })
      .sum('area');
    const percent = BigNumber(k)
      .div(BigNumber(totalarea))
      .times(100)
      .toFixed(2);

    return this.success({
      total: total,
      area: area,
      rest: rest,
      percent: percent,
    });
  }

  async kongzhiAction() {
    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();
    const kongzhi = [];
    const chuzu = [];
    const list = [];
    for (const item of project) {
      list.push(item.name);
      const k = await this.model('project_list')
        .where({ type: 4, project_id: item.id, status: 1 })
        .sum('area');
      kongzhi.push(k);
      const c = await this.model('project_list')
        .where({ type: 4, project_id: item.id, status: 2 })
        .sum('area');
      chuzu.push(c);
    }
    return this.success({ kongzhi: kongzhi, chuzu: chuzu, list: list });
  }

  async chengbenAction() {
    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();
    const data = [];
    for (const item of project) {
      const addData = {};

      addData.product = item.name;
      const p = await this.model('contract_list')
        .where({ type: 2, project_id: item.id })
        .field('wuye, fangzu')
        .find();
      addData['租金成本'] = BigNumber(p.wuye || 0)
        .plus(BigNumber(p.fangzu || 0))
        .toFixed(2);

      addData['装修成本'] = await this.model('output_list')
        .where({ type: 'base', project_id: item.id, status: 1 })
        .sum('money');

      addData['招商成本'] = await this.model('output_list')
        .where({ type: 'bussiness', project_id: item.id, status: 1 })
        .sum('money');
      // chuzu.push(c);

      data.push(addData);
    }
    return this.success(data);
  }

  async lineAction() {
    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();

    const days = [
      moment().subtract(11, 'months'),
      moment().subtract(10, 'months'),
      moment().subtract(9, 'months'),
      moment().subtract(8, 'months'),
      moment().subtract(7, 'months'),
      moment().subtract(6, 'months'),
      moment().subtract(5, 'months'),
      moment().subtract(4, 'months'),
      moment().subtract(3, 'months'),
      moment().subtract(2, 'months'),
      moment().subtract(1, 'months'),
      moment(),
    ];

    const data = {
      data: [],
    };

    const color = [
      '#5470c6',
      '#91cc75',
      '#fac858',
      '#ee6666',
      '#73c0de',
      '#3ba272',
      '#fc8452',
      '#9a60b4',
      '#ea7ccc',
    ];

    for (let i = 0; i < project.length; i++) {
      // for (const item of project) {
      const xData = [];
      const addData = {
        name: project[i].name,
        type: 'line',
        showSymbol: false,
        color: color[i],
        data: [],
      };

      for (const itemData of days) {
        xData.push(itemData.format('YYYY-MM'));
        const area = await this.model('project_list')
          .where({
            type: 4,
            status: 2,
            project_id: project[i].id,
            date_start: [
              'between',
              moment('2000-01-01').format('YYYY-MM-DD HH:MM'),
              itemData.endOf('month').format('YYYY-MM-DD HH:MM'),
            ],
          })
          .sum('zujin');
        addData.data.push(area);
      }
      data.data.push(addData);
      data.xData = xData;
      // data.push(addData);
    }
    return this.success(data);
  }

  async fenbuAction() {
    const arr = [
      { label: '0-100m²', value: ['between', [0, 100]] },
      { label: '100-200m²', value: ['between', [100.01, 200]] },
      { label: '200-300m²', value: ['between', [200.01, 300]] },
      { label: '300m²以上', value: ['between', [300.01, 10000]] },
    ];

    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();
    const label = [];
    const value = [];
    const legend = [];

    for (const itemProject of project) {
      label.push(itemProject.name);
    }

    for (const item of arr) {
      legend.push(item.label);
      const datas = [];
      for (const itemProject of project) {
        const c = await this.model('project_list')
          .where({
            type: 4,
            status: 1,
            area: item.value,
            project_id: itemProject.id,
          })
          .count();
        datas.push(c);
      }

      value.push({
        type: 'bar',
        data: datas,
        // coordinateSystem: 'polar',
        name: item.label,
        label: {
          show: true,
        },
        // stack: 'a',
        // emphasis: {
        //   focus: 'series',
        // },
      });
    }

    return this.success({
      label,
      value,
      legend,
    });
  }
  async hetongAction() {
    const arr = [
      { label: '1-2年', value: ['between', [0, 24]] },
      { label: '2-3年', value: ['between', [25, 36]] },
      { label: '3-4年', value: ['between', [37, 48]] },
      { label: '5年以上', value: ['between', [49, 1000]] },
    ];

    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();
    const label = [];
    const value = [];
    const legend = [];

    for (const itemProject of project) {
      label.push(itemProject.name);
    }

    for (const item of arr) {
      legend.push(item.label);
      const datas = [];
      for (const itemProject of project) {
        const c = await this.model('contract_list')
          .where({
            type: ['IN', '1,2'],
            status: 1,
            month: item.value,
            project_id: itemProject.id,
          })
          .count();
        datas.push(c);
      }

      value.push({
        type: 'bar',
        data: datas,
        // coordinateSystem: 'polar',
        name: item.label,
        label: {
          show: true,
        },
        // stack: 'a',
        // emphasis: {
        //   focus: 'series',
        // },
      });
    }

    return this.success({
      label,
      value,
      legend,
    });
  }
};
